<template>
  <div class="table">
    <el-table :data="props.tableData" size="small" stripe border>
      <template v-for="item in props.tableConfig.columnList" :key="item.prop || item.slot">
        <el-table-column v-if="item.slot" :label="item.label" :prop="item.prop" align="center" :min-width="item.minWidth || 100" :fixed="item.fixed">
          <template #default="scope">
            <slot :name="item.slot" :row="scope.row"></slot>
          </template>
        </el-table-column>
        <el-table-column v-else :prop="item.prop" :label="item.label" align="center" :min-width="item.minWidth || 100"></el-table-column>
      </template>
    </el-table>
    <el-pagination background v-if="props.tableConfig.total || props.tableConfig.total === 0" :page-sizes="[10, 20, 30, 40, 50]" :page-size="pageParams.pageSize" :currentPage="pageParams.pageIndex" @size-change="handleSizeChange" @current-change="handleCurrentChange" layout="total, sizes, prev, pager, next, jumper" :total="props.tableConfig.total"></el-pagination>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, watch } from 'vue'

export default defineComponent({
  name: 'tablePage',
  props: {
    tableConfig: [Object],
    tableData: [Array],
    pageIndex: Number
  },
  emits: ['changeParams'],
  setup (props, context) {
    const { pageIndex } = props
    watch(props, (newVal) => {
      pageParams.pageIndex = newVal.pageIndex
    })
    const handleSizeChange = (size) => {
      pageParams.pageSize = size
      pageParams.pageIndex = 1
      context.emit('changeParams', pageParams)
    }
    const handleCurrentChange = (pageIndex) => {
      pageParams.pageIndex = pageIndex
      context.emit('changeParams', pageParams)
    }
    let pageParams = reactive({
      pageIndex: pageIndex || 1,
      pageSize: 10
    })
    return {
      pageParams,
      props,
      handleSizeChange,
      handleCurrentChange
    }
  }
})

</script>

<style scoped lang="less">
  .el-pagination {
    text-align: right;
    margin-top: 15px;
    margin-right: 30px;
    &:deep(.el-pager) {
      li {
        border: 1px solid #DDDDDD;
        line-height: 26px;
      }
    }
  }

  .table {
    padding-bottom: 15px;
    background: #ffffff;
    border-radius: 1px;
    &:deep(.el-table__header-wrapper) {
      th {
        padding: 13px 0;
        color: #666666;
        font-size: 13px;
        font-weight: 400;
        border-color: #f2f2f2;
      }
    }
    &:deep(.el-table__body-wrapper) {
      td {
        padding: 0;
        height: 50px;
        color: #222222;
        font-size: 14px;
        font-weight: 400;
        border-color: #f2f2f2;
        .cell {
          padding: 0;
        }
      }
    }
  }
</style>
